﻿using DiverseStudents.UI;
using Sdk;
using System;
using UnityEngine;
using UnityEngine.UI;

namespace DiverseStudents.Mods
{
    internal class ModsUITopCell : IMosUICellBase
    {
        public HorizontalLayoutGroup topButtons;
        public ScrollRect topScroll;
        private RectTransform topScrollRect;
        private UIButton outCfg;
        private UIButton modCreate;
        private UIButton modTest;
        private Text title;

        public void Create()
        {
            topScroll = UICreate.ScrollRect("Top");
            if (topScroll == null)
            {
                return;
            }
            try
            {
                topScrollRect = topScroll.GetComponent<RectTransform>();
                topButtons = UICreate.CreatEmpty<HorizontalLayoutGroup>("TopButtonsGroup");
                topButtons.childForceExpandWidth = false;
                topButtons.childForceExpandHeight = false;
                topButtons.childControlHeight = false;
                topButtons.childControlWidth = false;
                topButtons.childAlignment = TextAnchor.MiddleRight;
                topButtons?.transform.SetParent(topScrollRect, false);
                title = UICreate.Text(TexTContent.topUITitle, UICreate.TextType.Tile, "ModListTitle");
                title?.transform.SetParent(topScrollRect, false);
                modCreate = UICreate.Button(OutConfig, ButtonsContent.topUmodCreate, flag: "ModCreate", type: false);
                modCreate?.transform.SetParent(topButtons.transform, false);
                modTest = UICreate.Button(OutConfig, ButtonsContent.topUImodTest, flag: "ModDownload", type: false);
                modTest?.transform.SetParent(topButtons.transform, false);
                outCfg = UICreate.Button(OutConfig, ButtonsContent.topUIoutCfg, flag: "OutCfg", type: false);
                outCfg?.transform.SetParent(topButtons.transform, false);
            }
            catch (Exception e)
            {
                DLog.Error(e);
            }
        }

        public void Update()
        {

        }

        public void OutConfig()
        {


            DLog.Log("OutCfg");
        }

        public void UpdateRect(RectTransform baseRect)
        {
            if (baseRect == null)
            {
                return;
            }
            Create();
            if (topScrollRect != null)
            {
                Vector2 center = new Vector2(0.5f, 0.5f);
                topScrollRect.anchorMax = center;
                topScrollRect.anchorMin = center;
                topScrollRect.sizeDelta = new Vector2(baseRect.rect.width * 0.975f, baseRect.rect.height * 0.08f);
                topScrollRect.anchoredPosition += new Vector2(-baseRect.rect.width / 2 + topScrollRect.rect.width * 0.01f, baseRect.rect.height / 2 - topScrollRect.rect.height / 2 - topScrollRect.rect.height * 0.17f);
                topScroll.transform.SetParent(baseRect, false);
                if (topButtons.transform != null)
                {
                    topButtons.spacing = topScrollRect.rect.width * 0.035f;
                    topButtons.padding = new RectOffset(0, Convert.ToInt16(topScrollRect.rect.width * 0.02f), 0, 0);
                    var topRect = topButtons.transform as RectTransform;
                    topRect.anchorMax = new Vector2(1, 1);
                    topRect.anchorMin = new Vector2(0, 0);
                    topRect.sizeDelta = new Vector2(0, topScrollRect.rect.height * 0.2f);
                    if (title != null)
                    {
                        var titleRect = title.transform as RectTransform;
                        titleRect.anchoredPosition += new Vector2(-topScrollRect.rect.width / 2, 0);
                        // title.fontSize = Convert.ToInt16(topScrollRect.rect.height * 0.5f);
                    }
                    //topRect.anchoredPosition += new Vector2(-topScrollRect.rect.width / 2, 0);
                }

                Vector2 rect = new Vector2(topScrollRect.rect.width * 0.06f, topScrollRect.rect.height * 0.5f);
                SetButtonFontSize(outCfg, rect);
                SetButtonFontSize(modTest, rect);
                SetButtonFontSize(modCreate, rect);
            }

        }


        private void SetButtonFontSize(UIButton button, Vector2 rect)
        {
            if (button != null)
            {
                button.transform.sizeDelta = rect;
                var txt = button.transform.GetComponentInChildren<Text>();
                if (txt != null)
                {
                    txt.fontSize = Convert.ToInt16(button.transform.rect.height * 0.4f);
                }
            }
        }

        public void Destroy()
        {
            if (topScrollRect != null)
            {
                GameObject.Destroy(topScrollRect.gameObject);
            }
        }
    }
}
